%%%-------------------------------------------------------------------
%% @doc robotServer public API
%% @end
%%%-------------------------------------------------------------------

-module(robotServer_app).

-behaviour(application).

-include("include/logger.hrl").
-include("include/player.hrl").
-include("include/myshare.hrl").

%% Application callbacks
-export([start/2, stop/1,prep_stop/1]).


%%====================================================================
%% API
%%====================================================================

start(_StartType, _StartArgs) ->
  {ok, SupPId} = robotServer_sup:start_link(),
  ?INFO("app server start \n"),
  myshare:init_env(),
  try
    robotServer_app_assist:start(),
    {ok, SupPId}
  catch
    Class : Error:Stacktrace ->
      ?ERROR("crash because class:~p,error:~p,stack:~p", [Class, Error, Stacktrace]),
      {error, Error}
  after
    timer:sleep(100)
  end.


%%--------------------------------------------------------------------
stop(_State) ->
  void.

%%====================================================================
%% Internal functions
%%====================================================================
prep_stop(_Arguments) ->
  Players = ets:tab2list(?ETS_PLAYER_INFO),
  io:format("Players :~p ~n",[Players]),
  Fun2 = fun(Player) ->
    Pid = Player#player_info.pid,
    io:format("player logout pid:~p ~n", [Pid]),
    gen_server:call(Pid, {stop})
         end,
  lists:foreach(Fun2, Players),
  timer:sleep(3000),
  ok.












